<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打印关于变量的易于理解的信息。</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.isset.html">isset</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.serialize.html">serialize</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.print-r" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">print_r</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">print_r</span> &mdash; <span class="dc-title">
     打印关于变量的易于理解的信息。
    </span></p>

   </div>
   <div class="refsect1 unknown-seealsp" id="refsect1-function.print-r-unknown-seealsp">
    <h3 class="title">描述</h3>
     <div class="methodsynopsis dc-description">
      <span class="type">bool</span> <span class="methodname"><strong>print_r</strong></span>
       ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$expression</code></span>
      [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$return</code></span>
     ] )</div>

    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      参数 <em><code class="parameter">return</code></em> 是在 PHP 4.3.0 的时候加上的
     </span>
    </p></blockquote>
    <p class="simpara">
     <span class="function"><strong>print_r()</strong></span> 显示关于一个变量的易于理解的信息。如果给出的是
     <span class="type"><a href="language.types.string.html" class="type string">string</a></span>、<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 
     或 <span class="type"><a href="language.types.float.html" class="type float">float</a></span>，将打印变量值本身。如果给出的是  
     <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，将会按照一定格式显示键和元素。<span class="type"><a href="language.types.object.html" class="type object">object</a></span>
     与数组类似。
    </p>
    <p class="simpara">
     记住，<span class="function"><strong>print_r()</strong></span> 将把数组的指针移到最后边。使用
     <span class="function"><a href="function.reset.html" class="function">reset()</a></span> 可让指针回到开始处。
    </p>
    <p class="para">
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$a&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/pre&gt;</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     上边的代码将输出：
     <div class="example-contents screen">
<div class="cdata"><pre>
&lt;pre&gt;
Array
(
    [a] =&gt; apple
    [b] =&gt; banana
    [c] =&gt; Array
        (
            [0] =&gt; x
            [1] =&gt; y
            [2] =&gt; z
        )
)
&lt;/pre&gt;
</pre></div>
     </div>
    </p>
    <p class="para">
     如果想捕捉 <span class="function"><strong>print_r()</strong></span> 的输出，可使用 
     <em><code class="parameter">return</code></em> 参数。若此参数设为
     <strong><code>TRUE</code></strong>，<span class="function"><strong>print_r()</strong></span> 将不打印结果（此为默认动作），而是返回其输出。
    </p>
    <p class="para">
     <div class="example" id="example-5157">
      <p><strong>Example #1 <em><code class="parameter">return</code></em> 参数示例</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$b&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'m'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">print_r&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//$results&nbsp;包含了&nbsp;print_r&nbsp;的输出结果<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      如果想在 PHP 4.3.0 之前的版本中捕捉
      <span class="function"><strong>print_r()</strong></span> 的输出，可使用<a href="ref.outcontrol.html" class="link">输出控制函数</a>。
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      在 PHP 4.0.4 之前的版本中，如果给出的
      <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 或 <span class="type"><a href="language.types.object.html" class="type object">object</a></span> 包含了直接或间接指向自身的引用，<span class="function"><strong>print_r()</strong></span>
      将永远继续下去。<em>print_r($GLOBALS)</em>
      就是一个例子，因为 <em>$GLOBALS</em>
      自身即是全局变量，其包含了指向自身的引用。
     </span>
    </p></blockquote>
    <p class="simpara">
      参见 <span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span>、<span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span>
      和 <span class="function"><a href="function.var-export.html" class="function">var_export()</a></span>。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.isset.html">isset</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.serialize.html">serialize</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
